/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.EnumIndexType;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.Arrays;

public class ElementEnumConstructGenerator extends ClassMethodGenerator {

    private String enumName;
    private IGspCommonField field;

    public ElementEnumConstructGenerator(String enumName) {
        this.enumName = enumName;
    }
    public ElementEnumConstructGenerator(String enumName, IGspCommonField field) {
        this.enumName = enumName;
        this.field=field;
    }
    @Override
    protected String getMethodName() {
        return enumName;
    }

    @Override
    protected TypeInfo getReturnType() {

        return null;
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        return new ArrayList<Modifier.ModifierKeyword>(
                Arrays.asList(new Modifier.ModifierKeyword[] {Modifier.ModifierKeyword.PRIVATE_KEYWORD}));
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        ParameterInfo info = new ParameterInfo();
        info.setParamName("value");
        ArrayList<ParameterInfo> params = new ArrayList<>();
        EnumIndexType enumIndexType=field.getEnumIndexType();
        if(enumIndexType==EnumIndexType.String){
            info.setParamType(new TypeInfo(String.class));
            params.add(info);
        }
        else {
            info.setParamType(new TypeInfo(Integer.class));
            params.add(info);
        }
        return params;
    }

    @Override
    protected Block buildMethodBody() {

        Block block=ast.newBlock();
        EnumIndexType enumIndexType = this.field.getEnumIndexType();
        Assignment assignment = this.ast.newAssignment();

        if (enumIndexType == EnumIndexType.String) {
            assignment.setLeftHandSide(this.ast.newSimpleName("strValue"));
        } else {

            assignment.setLeftHandSide(this.ast.newSimpleName("intValue"));
        }

        assignment.setOperator(Assignment.Operator.ASSIGN);
        assignment.setRightHandSide(this.ast.newSimpleName("value"));
        ExpressionStatement expressionStatement1 = this.ast.newExpressionStatement(assignment);

        block.statements().add(expressionStatement1);
        MethodInvocation methodInvocation=ast.newMethodInvocation();
        MethodInvocation methodInvocation1=ast.newMethodInvocation();

        methodInvocation1.setExpression(ast.newSimpleName(enumName));
        methodInvocation1.setName(ast.newSimpleName("getMappings"));
        methodInvocation.setExpression(methodInvocation1);

        methodInvocation.setName(ast.newSimpleName("put"));
        methodInvocation.arguments().add(ast.newSimpleName("value"));
        methodInvocation.arguments().add(ast.newThisExpression());
        ExpressionStatement expressionStatement2=ast.newExpressionStatement(methodInvocation);//第二个赋值

        block.statements().add(expressionStatement2);//

        return block;
    }

    @Override
    protected boolean buildIsConstructor() {
        return true;
    }
}
